/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.finance.ar.memo.dao.hibernate;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.redragon.framework.annotation.Permissions;
import org.redragon.framework.annotation.Permissions.PermissionType;
import org.redragon.framework.annotation.SqlParam;
import org.redragon.framework.hibernate.DaoSupport;
import org.redragon.framework.hibernate.model.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.erp.finance.ar.memo.dao.ArInvoiceMemoLineDao;
import com.erp.finance.ar.memo.dao.model.ArInvoiceMemoLine;
import com.erp.finance.ar.memo.dao.model.ArInvoiceMemoLineCO;

@Repository
public class ArInvoiceMemoLineDaoImpl implements ArInvoiceMemoLineDao{ 

    //注入DaoSupport工具类
    @Autowired
    private DaoSupport daoSupport;
    
    @Override
    public void insertDataObject(ArInvoiceMemoLine obj) {
        this.daoSupport.insertDataTransaction(obj);
    }

    @Override
    public void updateDataObject(ArInvoiceMemoLine obj) {
        this.daoSupport.updateDataTransaction(obj);
    }
    
    @Override
    public void insertOrUpdateDataObject(ArInvoiceMemoLine obj) {
        this.daoSupport.insertOrUpdateDataTransaction(obj);
    }

    @Override
    public void deleteDataObject(ArInvoiceMemoLine obj) {
        this.daoSupport.deleteDataTransactionJPA(obj);
    }

    @Override
    public List<ArInvoiceMemoLine> getDataObjects() {
        return this.daoSupport.getDataAllObject(ArInvoiceMemoLine.class);
    }

    @Override
    public ArInvoiceMemoLine getDataObject(int id) {
        return (ArInvoiceMemoLine)this.daoSupport.getDataObject(ArInvoiceMemoLine.class, id);
    }
    
    @Override
    public ArInvoiceMemoLine getDataObject(String code) {
        return null;
    }
    
    @Override
    public List<ArInvoiceMemoLine> getDataObjects(ArInvoiceMemoLineCO paramObj) {
        return null;
    }
    
    @Override
    public List<ArInvoiceMemoLine> getDataObjects(Pages pages) {
        return null;
    }
    
    @Override
    public List<ArInvoiceMemoLine> getDataObjects(Pages pages, ArInvoiceMemoLineCO paramObj) {
        return null;
    }

    @Override
    public List<Map<String, Object>> getDataObjectsArray(Pages pages, ArInvoiceMemoLineCO paramObj) {
        return null;
    }
    
    @Override
    @Permissions(PermissionType.DATA_AUTH)
    public List<ArInvoiceMemoLine> getDataObjectsForDataAuth(@SqlParam String dataAuthSQL, Pages pages, ArInvoiceMemoLineCO paramObj) {
        return null;
    }
    
    @Override
    public void deleteLineByHeadCode(String headCode) {
        String sql = "delete from ar_invoice_memo_line where invoice_head_code = :receiptheadcode";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("receiptheadcode", headCode);
        
        this.daoSupport.executeSQLTransaction(sql, args);
    }
    
    @Override
    public List<ArInvoiceMemoLine> getArInvoiceMemoLineListByHeadCode(Pages pages, ArInvoiceMemoLineCO paramObj) {
        String sql = "select p.* from ar_invoice_memo_line p where invoice_head_code=:receiptheadcode";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("receiptheadcode", paramObj.getInvoiceHeadCode());
        sql = sql + " order by p.invoice_line_id";
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("p", ArInvoiceMemoLine.class);
        
        return this.daoSupport.getDataSqlByPage(sql, entity, args, pages);
    }
    
    @Override
    public BigDecimal getArInvoiceMemoAmountByHeadCode(String headCode) {
        String sql = "select sum(l.amount) from ar_invoice_memo_line l where l.invoice_head_code = :receiptheadcode";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("receiptheadcode", headCode);
        
        List<BigDecimal> list = this.daoSupport.selectDataSqlCount(sql, args);
        if(list.size()>0) {
            return list.get(0);
        }
        
        return new BigDecimal(0);
    }
    
    @Override
    public BigDecimal[] getInvoiceMemoLineAmountSumByHeadCode(String headCode) {
        String sql = "select sum(amount) as amount,sum(tax_amount) as tax_amount from ar_invoice_memo_line l where l.invoice_head_code = :headCode";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("headCode", headCode);
        
        BigDecimal[] sumAmount = new BigDecimal[2];
        List<Object[]> list = this.daoSupport.selectDataSqlCount(sql, args);
        if(list.size()>0) {
            if(list.get(0)[0]!=null) {
                sumAmount[0] = new BigDecimal(String.valueOf(list.get(0)[0]));
            }else {
                sumAmount[0] = new BigDecimal(0D);
            }
            
            if(list.get(0)[1]!=null) {
                sumAmount[1] = new BigDecimal(String.valueOf(list.get(0)[1]));
            }else {
                sumAmount[1] = new BigDecimal(0D);
            }
        }
        
        return sumAmount;
    }
    
    @Override
    public List<ArInvoiceMemoLine> getArInvoiceMemoLineListByHeadCode(String headCode) {
        String sql = "select p.* from ar_invoice_memo_line p where invoice_head_code=:receiptheadcode";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("receiptheadcode", headCode);
        sql = sql + " order by p.invoice_line_id";
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("p", ArInvoiceMemoLine.class);
        
        return this.daoSupport.selectDataSql(sql, entity, args);
    }
    
    @Override
    public void updateLineForVersion(String code) {
        String sql = "update ar_invoice_memo_line set version = version+1 where invoice_head_code = :code";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("code", code);
        
        this.daoSupport.executeSQLTransaction(sql, args);
    }
    
    @Override
    public List<ArInvoiceMemoLine> getArInvoiceMemoLineByInvoiceCode(String invoiceCode) {
        String sql = "select p.* from ar_invoice_memo_line p,ar_invoice_memo_head h "
                   + "where p.invoice_head_code = h.invoice_head_code "
                   + "and h.status = 'CONFIRM' and h.approve_status = 'APPROVE' and h.invoice_source_head_code=:code ";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("code", invoiceCode);
        sql = sql + " order by p.invoice_line_id";
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("p", ArInvoiceMemoLine.class);
        
        return this.daoSupport.selectDataSql(sql, entity, args);
    }
    
}
